package net.keebook.webapp.bean;

import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.inject.Inject;

import net.keebook.webapp.dao.KeeBookDao;
import net.keebook.webapp.entity.KeeBook;
import net.keebook.webapp.model.SessionModel;
import net.keebook.webapp.util.MessageBundle;

import org.apache.log4j.Logger;


@ManagedBean
@RequestScoped
public class SelectKeeBookBean {
	private String keeBookId = "";
	private String masterPassword = "";
	private SessionModel sessionModel;
	
	@Inject
	private KeeBookDao keeBookDao;
	
	@PostConstruct
	public void init() throws IOException {
		setSessionModel(SessionModel.getInstance(FacesContext.getCurrentInstance()));
	}
	
	public void select() throws IOException {
		KeeBook keeBook = getKeeBookDao().get(getKeeBookId());
		
		if ((keeBook == null) || (!getKeeBookDao().canAccess(getSessionModel().getUserId(), keeBook.getId()))) {
			Logger.getLogger(this.getClass()).warn("Forbidden access to bean from userId="+getSessionModel().getUserId());
			FacesContext.getCurrentInstance().getExternalContext().redirect("403.xhtml");
			return;
		}
		
		getSessionModel().setKeeBookId(keeBook.getId());
		FacesContext.getCurrentInstance().getExternalContext().redirect("index.xhtml");
	}
	
	public List<KeeBook> getBoxes() {
		return getKeeBookDao().getList(getSessionModel().getUserId());
	}
	
	public String getKeeBookId() {
		return keeBookId;
	}
	public void setKeeBookId(String keeBookId) {
		this.keeBookId = keeBookId;
	}
	public KeeBookDao getKeeBookDao() {
		return keeBookDao;
	}
	public void setKeeBookDao(KeeBookDao keeBookDao) {
		this.keeBookDao = keeBookDao;
	}

	public String getMasterPassword() {
		return masterPassword;
	}

	public void setMasterPassword(String masterPassword) {
		this.masterPassword = masterPassword;
	}
	
	public void validateKeeBookId(FacesContext context, UIComponent component, Object value) {
		String id = (String)value;
		KeeBook keeBook = getKeeBookDao().get(id);
		
		if (keeBook == null) {
			((UIInput)component).setValid(false);
			context.addMessage(component.getClientId(context), new FacesMessage(MessageBundle.get().getString("error.keeBookNotFound")));
			return;
		}
		if (!getKeeBookDao().canAccess(getSessionModel().getUserId(), keeBook.getId())) {
			((UIInput)component).setValid(false);
			context.addMessage(component.getClientId(context), new FacesMessage(MessageBundle.get().getString("error.accessDenied")));
			return;
		}
	}
	
	public void validateMasterPassword(FacesContext context, UIComponent component, Object value) {
		UIInput boxKeyInput = (UIInput)context.getViewRoot().findComponent("selectKeeBookForm:keeBookId");
		String keeBookId = (String)boxKeyInput.getValue();
		if (keeBookId == null || "".equals(keeBookId)) {
			return;
		}
		
		String checksum = (String)value;
		KeeBook keeBook = getKeeBookDao().get(keeBookId);
		
		if (keeBook == null || !keeBook.getKeyphraseChecksum().equals(checksum)) {
			((UIInput)component).setValid(false);
			context.addMessage(component.getClientId(context), new FacesMessage(MessageBundle.get().getString("error.wrongPassword")));
		}
	}

	public SessionModel getSessionModel() {
		return sessionModel;
	}

	public void setSessionModel(SessionModel sessionModel) {
		this.sessionModel = sessionModel;
	}
}
